package com.linxz.ui.searchview;
import android.animation.AnimatorSet;
import android.animation.FloatEvaluator;
import android.animation.IntEvaluator;
import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.LinearInterpolator;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.linxz.ui.GuiHelper;
import com.linxz.ui.R;
/**
 * @author Linxz
 * 创建日期：2020年02月21日 23:12
 * version：v4.5.4
 * 描述：
 */
public class SearchEditText extends FrameLayout {

    private TextView tvCancel;
    private LinearLayout layoutSearch;
    private EditText edtSearch;
    private ImageView imgDelete;

    public SearchEditText(@NonNull Context context) {
        super(context);
    }

    public SearchEditText(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context,attrs,0);
    }

    public SearchEditText(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
        setListener();
    }

    private void init(){
        LayoutInflater.from(getContext()).inflate(R.layout.ui_search_view,this);
        tvCancel=findViewById(R.id.tvCancel);
        imgDelete=findViewById(R.id.imgDelete);
        layoutSearch=findViewById(R.id.layoutSearch);
        edtSearch=findViewById(R.id.edtSearch);
        imgDelete=findViewById(R.id.imgDelete);
        startAnimation();
    }

    @SuppressLint("ClickableViewAccessibility")
    public void setListener(){
        edtSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!TextUtils.isEmpty(edtSearch.getText().toString().trim())){
                    imgDelete.setVisibility(View.VISIBLE);
                }else{
                    imgDelete.setVisibility(View.INVISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        edtSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (mOnSearchViewListener!=null){
                    mOnSearchViewListener.onEditorAction(v,actionId,event);
                    edtSearch.clearFocus();
                    imgDelete.requestFocus();
                }
                return false;
            }
        });
        edtSearch.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                tvCancel.setVisibility(View.VISIBLE);
                return false;
            }
        });
        imgDelete.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                edtSearch.setText("");
            }
        });
        tvCancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnSearchViewListener!=null){
                    mOnSearchViewListener.onCancelClick();
                }
                Activity act=GuiHelper.getActivity(getContext());
                if (act!=null){
                    act.finish();
                }
            }
        });
        tvCancel.setOnLongClickListener(new OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (mOnSearchViewListener!=null){
                    mOnSearchViewListener.onCancelLongClick();
                }
                return false;
            }
        });

        imgDelete.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnSearchViewListener!=null){
                    mOnSearchViewListener.onViewEmpty();
                }
            }
        });
    }

    private void startAnimation() {
        layoutSearch.post(new Runnable() {
            @Override
            public void run() {
                ViewMarginWrapper viewWrapper=new ViewMarginWrapper(layoutSearch);
               // ObjectAnimator leftMarginAnimation = initMarginAnimation(viewWrapper, "leftMargin", 0, ivBack.getWidth(), 300L);
                ObjectAnimator rightMarginAnimation = initMarginAnimation(viewWrapper, "rightMargin", 0, tvCancel.getWidth(), 500L);
                ObjectAnimator searchTextAnimation = initTranslationAnimation(tvCancel, "translationX", 500L, 200f, 0f);
               // ObjectAnimator backImgViewAnimation = initTranslationAnimation(ivBack, "translationX", 300L, -100f, 0f);
                AnimatorSet animatorSet = new AnimatorSet();
                animatorSet.playTogether(rightMarginAnimation, searchTextAnimation);
               // animatorSet.playTogether(leftMarginAnimation, rightMarginAnimation, searchTextAnimation, backImgViewAnimation);
                animatorSet.setDuration(500);
                animatorSet.start();
            }
        });

    }


    private  ObjectAnimator initMarginAnimation(
            Object obj,
            String propertyName,
            int startMargin,
            int endMargin,
            Long duration){
        ObjectAnimator objectAnimator = ObjectAnimator.ofObject(
                obj,
                propertyName,
                new IntEvaluator(),
                startMargin,
                endMargin
        );
        objectAnimator.setDuration(duration);
        objectAnimator.setInterpolator(new LinearInterpolator());
        return objectAnimator;
    }

    private ObjectAnimator initTranslationAnimation(Object target, String propertyName, Long duration, Float start, Float end){
        ObjectAnimator objectAnimator = ObjectAnimator.ofObject(target, propertyName, new FloatEvaluator(), start, end);
        objectAnimator.setDuration(duration);
        objectAnimator.setInterpolator(new DecelerateInterpolator());
        return objectAnimator;
    }



    private OnSearchViewListener mOnSearchViewListener;
    public void setOnSearchViewListener(OnSearchViewListener onSearchViewListener){
        this.mOnSearchViewListener=onSearchViewListener;
    }
    public interface OnSearchViewListener{
        void onCancelClick();
        void onCancelLongClick();
        void onViewEmpty();
        void onEditorAction(TextView v, int actionId, KeyEvent event);
    }


}
